package com.song.controller;


import com.alibaba.fastjson.JSON;
import com.song.entity.vo.UserBindVO;
import com.song.hfb.RequestHelper;
import com.song.result.Result;
import com.song.service.UserBindService;
import com.song.util.JwtUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 前端控制器
 * </p>
 *
 * @author Song
 * @since 2025-01-03
 */
@RestController
@RequestMapping("/api/core/userBind")
public class UserBindController {

    @Autowired
    private UserBindService userBindService;

    // /api/core/userBind/auth/bind

    @PostMapping("/auth/bind")
    public Result bind(@RequestBody UserBindVO userBindVO, HttpServletRequest request){
        String token = request.getHeader("token");
        //通过token获取userId
        Long userId = JwtUtils.getUserId(token);
        //如果  userId  == null  没有登录   判断自己写 !!!
       String formStrss= userBindService.commitBindUser(userBindVO,userId);

        return  Result.ok().data("formStr",formStrss);
    }


    //http://localhost/api/core/userBind/notify
    @ApiOperation("账户绑定的异步回调功能")
    @PostMapping("/notify")
    public  String notifys(HttpServletRequest request){
        Map<String, Object> paramMap = RequestHelper.switchMap(request.getParameterMap());

        System.out.println("====>:"+JSON.toJSONString(paramMap));
        //校验签名
        if(!RequestHelper.isSignEquals(paramMap)){
             return "fail";
        }

        //修改绑定状态
        userBindService.notifys(paramMap);

        return "success";


    }

}

